package com.yc.base.service.impl;

import com.yc.base.dao.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional
public abstract class BaseService<T, ID extends Serializable> {

	@Autowired
	protected BaseRepository<T, ID> baseRepository;


	public T update(T m) {
		T t = this.baseRepository.save(m);
		return t;
	}


	public List<T> updateList(List<T> data) {
		try {
			List<T> list = this.baseRepository.saveAll(data);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public boolean delete(T m) {
		try {
			this.baseRepository.delete(m);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delete(ID id) {
		try {
			this.baseRepository.deleteById(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean deleteIds(List<ID> ids) {
		try {
			for (ID id : ids) {
				this.delete(id);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<T> findAll(Sort var1) {
		return baseRepository.findAll(var1);
	}

}
